#!/bin/bash

echo "🎭 Starting Mock Services for Spring AI Agent Platform"
echo "========================================================"
echo ""

# 检查Node.js
if ! command -v node &> /dev/null; then
    echo "❌ Node.js not found. Please install Node.js first."
    exit 1
fi

# 检查npm
if ! command -v npm &> /dev/null; then
    echo "❌ npm not found. Please install npm first."
    exit 1
fi

# 安装依赖
if [ ! -d "node_modules" ]; then
    echo "📦 Installing dependencies..."
    npm install
    echo ""
fi

# 停止已有进程
echo "🔍 Checking for existing mock services..."
lsof -ti:4000 | xargs kill -9 2>/dev/null && echo "  Stopped existing ContextManager on port 4000"
lsof -ti:4001 | xargs kill -9 2>/dev/null && echo "  Stopped existing ToolManager on port 4001"
echo ""

# 启动服务
echo "🚀 Starting mock services..."
echo ""

# 启动ContextManager
node mock-context-manager.js &
CONTEXT_PID=$!

# 等待一下
sleep 1

# 启动ToolManager
node mock-tool-manager.js &
TOOL_PID=$!

# 等待服务启动
sleep 2

echo ""
echo "================================================"
echo "✅ Mock Services Started!"
echo "================================================"
echo ""
echo "Context Manager:"
echo "  PID: $CONTEXT_PID"
echo "  URL: http://localhost:4000"
echo "  Test: curl http://localhost:4000/health"
echo ""
echo "Tool Manager:"
echo "  PID: $TOOL_PID"
echo "  URL: http://localhost:4001"
echo "  Test: curl http://localhost:4001/health"
echo ""
echo "To stop: kill $CONTEXT_PID $TOOL_PID"
echo "Or press Ctrl+C"
echo ""

# 等待信号
trap "echo ''; echo 'Stopping mock services...'; kill $CONTEXT_PID $TOOL_PID 2>/dev/null; exit 0" SIGINT SIGTERM

# 保持运行
wait
